package com.harmony.suppertiger.slice;

import com.harmony.suppertiger.CustomControlBar;
import com.harmony.suppertiger.MySupperTiggerView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class MainAbilitySlice extends AbilitySlice {
    private DirectionalLayout myLayout = new DirectionalLayout(this);
    private MySupperTiggerView mySupperTiggerView;
    private MyEventHandler handler;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "SupperTigger");

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        drawComponent();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    // 自定义组件1
    public void drawComponent(){
        // 声明布局
        DependentLayout.LayoutConfig config = new DependentLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT);
        config.setMargins(0, 0, 0, 0);
        myLayout.setWidth(MATCH_PARENT);
        myLayout.setHeight(MATCH_PARENT);
        myLayout.setOrientation(Component.VERTICAL);


        mySupperTiggerView = new MySupperTiggerView(this);
        //事件处理实现类
        EventRunner eventRunner = EventRunner.current();
        handler = new MyEventHandler(eventRunner);
        //设置背景颜色
        ShapeElement shapeElement2 = new ShapeElement();
        RgbColor rgbColor2 = new RgbColor( 219,112,147);
        shapeElement2.setRgbColor(rgbColor2);
        mySupperTiggerView.setBackground(shapeElement2);
        mySupperTiggerView.setLayoutConfig(config);
        mySupperTiggerView.setWidth(MATCH_PARENT);
        mySupperTiggerView.setHeight(MATCH_PARENT);
        myLayout.addComponent(mySupperTiggerView);
        super.setUIContent(myLayout);
    }
    class MyEventHandler extends EventHandler {

        public MyEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            int eventId = event.eventId;
            switch (eventId) {
                case 2:
                    System.out.println("刷新了======================");
                    if (mySupperTiggerView != null) {
                        mySupperTiggerView.invalidate();
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
